#!/bin/bash

set -e
LANG=en_US.UTF-8

inst=

LOG_ERROR() {
    local content=${1}
    echo -e "\033[31m[ERROR] ${content}\033[0m"
}

help() {
    echo "$1"
    cat <<EOF
Usage:
    -n  <service-name>                   [Required]   set service name
e.g
    bash $0 -n port-service-A
EOF
    exit 0
}

parse_command() {
    while getopts "n:h" option; do
        # shellcheck disable=SC2220
        case ${option} in
        n)
            inst=$OPTARG
            ;;
        h) help ;;
        esac
    done    
}

deploy_all() {
    if [ -z "${inst}" ]; then
        LOG_ERROR "Please set [service-name]"
        help
        exit 1
    fi

    echo creating "${inst}" ...
    dst=${PWD}/instances/$inst

    mkdir -p ${dst}/apps
    mkdir -p ${dst}/conf
    mkdir -p ${dst}/db
    mkdir -p ${dst}/lib

    cp apps/*.jar ${dst}/apps/
    cp conf/application-sample.toml ${dst}/conf/application-sample.toml
    cp conf/application.properties ${dst}/conf/application.properties
    cp lib/*.jar ${dst}/lib/
    cp start.sh ${dst}/
    cp stop.sh ${dst}/
}

parse_command $@
deploy_all
